package br.ufpe.cin.rdfilter.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import br.ufpe.cin.rdfilter.feedback.Triple;

public class LoadQuery {

	private File dir;
	private ArrayList<Triple> arrayTriple = new ArrayList<Triple>();
	private List<String> queries = new ArrayList<String>();

	public LoadQuery(File dir) {
		this.dir = dir;
	}

	public File getDir() {
		return dir;
	}

	public void setDir(File dir) {
		this.dir = dir;
	}

	public ArrayList<Triple> getArrayTriple() {
		return arrayTriple;
	}

	public void setArrayTriple(ArrayList<Triple> arrayTriple) {
		this.arrayTriple = arrayTriple;
	}
	
	public List<String> getQueries(){
		return queries;
	}
	
	public void setQueries(List<String> queries){
		this.queries=queries;
	}

	public List<String> loadQuery(){
		
		try {
			FileReader reader = new FileReader(dir);
			BufferedReader in = new BufferedReader(reader);
			String line = null;

			while ((line = in.readLine()) != null) {
				queries.add(line);
			}
			for (int i=0;i<queries.size();i++)
				System.out.println(queries.get(i));
			
			in.close();
			reader.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return queries;
	}
	public void load() {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			Document doc;
			doc = db.parse(dir);
			// Passo 1: obter o elemento raiz
			Element raiz = doc.getDocumentElement();
			// Passo 2: localizar os elementos filhos da agenda
			NodeList listaHead = raiz.getElementsByTagName("head");
			Element head = (Element) listaHead.item(0);
			NodeList listaVariable = head.getElementsByTagName("variable");
			for (int i = 0; i < listaVariable.getLength(); i++) {
				Element elementoVariable = (Element) listaVariable.item(i);
				Attr nameVariable = elementoVariable.getAttributeNode("name");
			}
			NodeList listaResults = raiz.getElementsByTagName("results");
			Element results = (Element) listaResults.item(0);
			NodeList listaResult = results.getElementsByTagName("result");
			for (int i = 0; i < listaResult.getLength(); i++) {
				arrayTriple.add(criaTripla((Element) listaResult.item(i)));
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Triple criaTripla(Element elemento) {

		Triple tripla = new Triple();

		NodeList listaBind = elemento.getElementsByTagName("binding");

		Element bindS = (Element) listaBind.item(0);
		NodeList listaUriS = bindS.getElementsByTagName("uri");
		Element uriS = (Element) listaUriS.item(0);
		Node noS = uriS.getFirstChild();
		tripla.setSubject(noS.getNodeValue());

		Element bindP = (Element) listaBind.item(1);
		NodeList listaUriP = bindP.getElementsByTagName("uri");
		Element uriP = (Element) listaUriP.item(0);
		Node noP = uriP.getFirstChild();
		tripla.setPredicate(noP.getNodeValue());

		Element bindO = (Element) listaBind.item(2);
		// Attr objeto = bindO.getAttributeNode("objeto");
		NodeList listaLiteral = bindO.getElementsByTagName("literal");
		Element literal = (Element) listaLiteral.item(0);
		// Attr objeto = literal.getAttributeNode("datatype");
		Node noO = literal.getFirstChild();
		tripla.setObject(noO.getNodeValue());

		return tripla;
	}
}
